package com.tpc.wdt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpc.wdt.WdtClient;
import com.tpc.wdt.utils.JDBCUtil;
import com.tpc.wdt.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.management.Query;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Slf4j
@Service
@Transactional
public class GoodsPush {


    @Value("${wdt.sid}")
    private String sid;

    @Value("${wdt.appkey}")
    private String appkey;

    @Value("${wdt.appsecret}")
    private String appsecret;

    @Value("${wdt.url}")
    private String url;

    @Value("${wdt.goods.sql}")
    private String goodsSql;



    public void goodsPush()  {
        WdtClient client = new WdtClient(sid, appkey, appsecret, url);


        Connection connection = JDBCUtil.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String isUpload = "";
        String uploadInfo = "";
        try {
//            log.info("==={}===",goodsSql);
            Map<String, Object> result = queryRunner.query(connection, goodsSql, new MapHandler());
            String valueInfo = JSON.toJSONString(result);
//            log.info("--{}--",valueInfo);
            if(valueInfo.equals("null")) {
                return;
            }
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(JSONUtil.transToLowerObject(valueInfo));
//            log.info("--{}--",jsonObject);
            Map<String, Object>[] goods_list = new Map[1];
            Map<String, Object>[] spec_list = new Map[1];
            spec_list[0] = new HashMap<String, Object>();
            spec_list[0].put("spec_no", jsonObject.getString("spec_no"));
//            spec_list[0].put("spec_code",  jsonObject.getString("spec_code"));
            spec_list[0].put("spec_name", jsonObject.getString("spec_name"));
            spec_list[0].put("barcode", jsonObject.getString("barcode"));
//            spec_list[0].put("prop1", jsonObject.getString("prop1"));
//            spec_list[0].put("prop2", jsonObject.getString("prop2"));
//            spec_list[0].put("prop3", jsonObject.getString("prop3"));
//            spec_list[0].put("prop4", jsonObject.getString("prop4"));
//            spec_list[0].put("prop6", jsonObject.getString("prop6"));
            spec_list[0].put("retail_price", jsonObject.getString("retail_price"));

            goods_list[0] = new HashMap<String,Object>();
            goods_list[0].put("goods_no", jsonObject.getString("goods_no"));
//            goods_list[0].put("goods_type",jsonObject.getString("goods_type"));
            goods_list[0].put("goods_name",jsonObject.getString("goods_name"));
            goods_list[0].put("origin",jsonObject.getString("origin"));
//            goods_list[0].put("class_name",jsonObject.getString("class_name"));
//            goods_list[0].put("brand_name",jsonObject.getString("brand_name"));
            goods_list[0].put("unit_name",jsonObject.getString("unit_name"));
            goods_list[0].put("props1",jsonObject.getString("props1"));
            goods_list[0].put("props3",jsonObject.getString("props3"));
//            goods_list[0].put("alias",jsonObject.getString("alias"));
//            goods_list[0].put("props5",jsonObject.getString("props5"));
//            goods_list[0].put("props6",jsonObject.getString("props6"));
            goods_list[0].put("spec_list", spec_list);

            //通过第三方json解析工具类fastjson将map解析成json
            String goods_list_json = JSON.toJSONString(goods_list);
            //System.out.println(goods_list_json);

            Map<String, String> params = new HashMap<String, String>();
            params.put("goods_list", goods_list_json);
            try {
                log.info("商品请求信息：{}",JSONObject.toJSONString(params));
                String response = client.execute("goods_push.php", params);
                log.info("商品返回信息：{}",response);
                JSONObject jsonResponse = JSONObject.parseObject(response);
                uploadInfo = jsonResponse.getString("message");
                if(jsonResponse.getInteger("code")==0){
                    isUpload = "Y";
                }else{
                    isUpload = "N";
                }
//                String updateSql = "update GoodsDoc set ISWDT = '" +isUpload + "',WDTINFO = '" + uploadInfo + "' where 'DYF'||'-'||goodscode = '" + jsonObject.getString("goods_no") +"'";
                String updateSql = "update GoodsDoc set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where goodscode = '" + jsonObject.getString("goods_no") +"'";
                queryRunner.update(connection,updateSql);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection);
        }
    }
}
